package com.lin.service.impl;

import cn.hutool.system.UserInfo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lin.base.Identity;
import com.lin.entity.User;
import com.lin.mapper.UserMapper;
import com.lin.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 林伟鸿
 * @since 2022-04-14
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    UserMapper userMapper;

    @Resource
    BCryptPasswordEncoder bCryptPasswordEncoder;

    @Override
    public void register(User user) {
        if (userMapper.selectOne(new QueryWrapper<User>().eq(User.USERNAME, user.getUsername())) != null) {
            throw new RuntimeException("账号已经存在！");
        }else {
            if (user.getIdentity() == null) user.setIdentity(0);
            user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
            userMapper.insert(user);
        }
    }
}

